package com.peanut.baby.mvp.subscribe;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.peanut.baby.R;
import com.peanut.baby.model.PTag;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;

import java.util.ArrayList;
import java.util.List;

public class TagSubscribeAdapter extends TagAdapter<PTag> {
    private Context ctx;
    private LayoutInflater inflater;
    private onItemCheckChangedListener listener;
    public TagSubscribeAdapter(Context ctx,List<PTag> datas) {
        super(datas);
        this.ctx = ctx;
        inflater = LayoutInflater.from(this.ctx);
    }

    @Override
    public View getView(FlowLayout parent, int position, final PTag o) {
        View item = inflater.inflate(R.layout.layout_item_tag_subscribe,null, false);
        CheckBox view = item.findViewById(R.id.subscribe_checkbox);
        view.setChecked(o.status == 1);
        view.setText(o.name);
        view.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                o.status = isChecked? 1:0;
                if(listener != null){
                    listener.onItemCheckChanged();
                }

            }
        });
        return item;
    }

    public boolean hasCheckedItem(){
        boolean flag = false;
        for(int i=0;i< getCount();i++){
            if(getItem(i).status == 1){
                flag = true;
                break;
            }

        }
        return flag;
    }

    public List<PTag> getCheckedList(){
        List<PTag> result = new ArrayList<>();
        for(int i=0;i< getCount();i++){
            if(getItem(i).status == 1){
                result.add(getItem(i));
            }

        }
        return result;
    }

    public void setListener(onItemCheckChangedListener listener) {
        this.listener = listener;
    }

    public interface onItemCheckChangedListener{
        void onItemCheckChanged();
    }
}
